﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;
using System.Drawing;

namespace GLPong
{
    class Ball : IPhysicalDrawable
    {
        float _x, _y;
        float _radius;
        Color _color;

        public float Radius { get { return _radius; } }
        public float X { get { return _x; } set { _x = value; } }
        public float Y { get { return _y; } set { _y = value; } }

        OpenTK.Vector3d _speed;
        public OpenTK.Vector3d Speed { get { return _speed; } set { _speed = value; } }

        const int segments = 256;

        public Ball(float x, float y, float radius, Color color)
        {
            _x = x;
            _y = y;
            _radius = radius;
            _color = color;

            _speed = Math.Cos(Math.PI / 6) * OpenTK.Vector3d.UnitX + Math.Sin(Math.PI / 6) * OpenTK.Vector3d.UnitY;
        }

        void IDrawable.Display()
        {
            float angle = 0;

            GL.Begin(BeginMode.Polygon);
            GL.Color3(_color);
            for (int i = 0; i < segments; i++)
            {
                angle = (float)(2 * Math.PI * i / segments);
                GL.Vertex3(_x + _radius * Math.Cos(angle), _y + _radius * Math.Sin(angle), 0);
            }
            GL.End();
        }

        public void UpdatePos(long ticks)
        {
            _x += (float)(_speed.X * ticks / 200000);
            _y += (float)(_speed.Y * ticks / 200000);
        }

        public void ChangeSpeed(OpenTK.Vector3d speed)
        {
            _speed = speed;
        }
    }
}
